package com.ht.medicaltreatment.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.BorrowUpdateAdapter
import com.ht.medicaltreatment.bean.LoanSample
import com.ht.medicaltreatment.view.TypeDialog
import com.ht.medicaltreatment.viewmodel.BorrowBuildViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_build_borrow_add_layout.*

//借出表单  添加借出计划
class BorrowAddActivity:BaseActivity(), View.OnClickListener, OnItemClickListener {
    override fun getLayoutId() = R.layout.activity_build_borrow_add_layout

    private lateinit var projectId:String
    private var typeId = ""
    private val model by viewModels<BorrowBuildViewModel>()
    //private val adapter = BorrowBuildAddAdapter()
    private val adapter = BorrowUpdateAdapter()
    private var orderId = "-1"
    private var count = 0

    override fun init() {
        setTitle("创建计划表单")

        tvProjectName.text = intent.getStringExtra("projectName")
        projectId = intent.getStringExtra("projectId")?:""
        tvName.text = intent.getStringExtra("userName")
        tvReturnTime.text = intent.getStringExtra("returnTime")
        etName.text = intent.getStringExtra("orderName")

        tvProjectName.setOnClickListener(this)
        tvName.setOnClickListener(this)
        btnStart.setOnClickListener(this)
        imgDelete.setOnClickListener(this)
        cbAll.setOnClickListener(this)
        tvReturnTime.setOnClickListener(this)
        tvSample.setOnClickListener(this)
        tvSampleBox.setOnClickListener(this)
        tvQuery.setOnClickListener(this)
        tvType.setOnClickListener(this)
        btnScan.setOnClickListener(this)

        ClickUtils.expandClickArea(imgDelete,30)

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
    }

    override fun onResume() {
        super.onResume()
        initData()
    }

    private fun initData() {
        try {
            if(orderId == "-1"){
                orderId = intent.getStringExtra("id")?:""
            }
            //样本列表数据
            model.detailBorrow(orderId.toInt()) {
                adapter.setNewInstance(it.loanSampleList)
                count = it.loanSampleList.size
                if (tvCheckCount != null) {
                    tvCheckCount.text = "$count"
                }
                dismissProgress()
            }
        }catch (e : Exception){
            LogUtils.i(e.message)
        }
    }

    override fun isViewStup() = true

    @SuppressLint("NotifyDataSetChanged")
    override fun onClick(v: View?) {
        when(v){
            cbAll -> {
                adapter.data.forEach {
                    it.isChecked = cbAll.isChecked
                }
                if(cbAll.isChecked){
                    tvCheckCount.text = "${adapter.data.size}"
                }else{
                    tvCheckCount.text = "0"
                }
                adapter.notifyDataSetChanged()
            }

            //删除
            imgDelete -> {
                val list = adapter.data.filter { it.isChecked }
                if(list.isEmpty()){
                    return
                }

                showProgress()
                val sb = StringBuffer()
                list.forEach {
                    sb.append("${it.id},")
                }
                val ids = sb.toString().substring(0,sb.toString().length - 1)
                model.deleteSmaple(ids) {
                    dismissProgress()
                    if (adapter.data.removeIf { it.isChecked }) {
                        adapter.notifyDataSetChanged()
                        showToast("已删除")
                        count -= list.size
                        tvCheckCount.text = "$count"
                    }
                }
            }

            tvQuery -> {
                llType.visibility = View.VISIBLE
            }

            tvSample -> {
                llType.visibility = View.GONE
                tvQuery.text = "样本"
            }

            tvSampleBox->{
                llType.visibility = View.GONE
                tvQuery.text = "样本盒"
            }

            //样本类别
            tvType -> {
                TypeDialog(this).show { id, name ->
                    tvType.text = name
                    typeId = id
                }
            }

            //查询
            btnStart -> {
                val intent = Intent(this,BorrowQueryActivity::class.java)
                intent.putExtra("projectId",projectId)
                intent.putExtra("sampleName",etSampleName.text.toString())
                intent.putExtra("sampleCategoryId",typeId)
                intent.putExtra("orderId",orderId)
                intent.putExtra("type",if (tvQuery.text.toString() == "样本")  "0" else "1")
                startActivityForResult(intent){
                    showProgress()
                    initData()
                }
            }

            //去盘点
            btnScan -> {
                val intent = Intent(this,BorrowOrderDetailActivity::class.java)
                intent.putExtra("id",orderId.toInt())
                startActivity(intent)
                finish()
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as LoanSample
        item.isChecked = !item.isChecked
        adapter.notifyItemChanged(position)
        val list = adapter.data as MutableList<LoanSample>
        val bean = list.find { !it.isChecked }

        if(bean == null){
            cbAll.isChecked = true
        }else{
            if(cbAll.isChecked){
                cbAll.isChecked = false
            }
        }
    }
}

